package com.itheima.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {


    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;


    //@Override
    //public PageResult page(Integer page, Integer pageSize) {
//        //获取总记录数
//        Long total =  empMapper.count();
//        //获取结果列表
//        Integer start = (page-1) *pageSize;
//        List<Emp> empList = empMapper.list(start,pageSize);
//        return new PageResult(total,empList);

    //1.设置分页参数
//        PageHelper.startPage(page,pageSize);
//
//        //2.执行查询
//        List<Emp> emplist = empMapper.list();
//        Page<Emp> p = (Page<Emp>) emplist;
//
//        //3.封装结果
//        return new PageResult(p.getTotal(),p.getResult());
//    }

    //    @Override
//    public PageResult page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
//        //1.设置分页参数
//        PageHelper.startPage(page,pageSize);
//
//        //2.执行查询
//        List<Emp> emplist = empMapper.list(name,gender,begin,end);
//        Page<Emp> p = (Page<Emp>) emplist;
//
//        //3.封装结果
//        return new PageResult(p.getTotal(),p.getResult());
//    }
    @Override
    public PageResult page(EmpQueryParam  empQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        //2.执行查询
        List<Emp> emplist = empMapper.list(empQueryParam);
        Page<Emp> p = (Page<Emp>) emplist;

        //3.封装结果
        return new PageResult(p.getTotal(), p.getResult());
    }


    @Override
    public void delete(Integer[] ids) {
        empMapper.deleteEmp(ids);
        empExprMapper.deleteExpr(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        //保存员工的基本信息
        empMapper.addEmp(emp);

        //保存员工的工作经历
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public Emp getInfo(Integer id) {
       Emp emp = empMapper.getInfo(id);
       List<EmpExpr> exprList = empExprMapper.getExprInfo(id);
       emp.setExprList(exprList);
       return emp;
    }
}



